package com.example.rg1anotherbroadcastreceiver;

import androidx.appcompat.app.AppCompatActivity;

import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button button1;
    private AnotherReceiver anotherReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1 = findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //注册广播接收者
                IntentFilter intentFilter = new IntentFilter();
                //要接收的广播
                intentFilter.addAction("com.example.broadcast.MY_BROADCAST");
                anotherReceiver = new AnotherReceiver();
                //注册接收者
                registerReceiver(anotherReceiver,intentFilter);
            }
        });
    }
    @Override
    protected void onDestroy() {
        //取消注册
        unregisterReceiver(anotherReceiver);
        super.onDestroy();
    }
}
